package com.share.module.business.stockprofit.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.module.business.stock.dao.StockDAO;
import com.share.module.business.stock.entity.Stock;
import com.share.module.business.stockprofit.dao.StockProfitDAO;
import com.share.module.business.stockprofit.entity.StockProfit;
import com.share.module.business.stockprofit.vo.StockProfitPageQueryVO;
import com.share.module.business.stockprofit.vo.StockProfitPageVO;
import com.stock.common.lang.StringUtils;
import com.stock.mybatisplus.utils.PageUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 股票净利润 服务类
 * </p>
 *
 * @author lijy
 * @since 2025-11-07
 */
@Service
public class StockProfitService extends ServiceImpl<StockProfitDAO, StockProfit> {

    @Resource
    private StockDAO stockDAO;

    /**
     * 分页查询股票净利润列表
     */
    public PageUtils<StockProfitPageVO> page(StockProfitPageQueryVO vo) {
        LambdaQueryWrapper<StockProfit> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotBlank(vo.getStockCode()), StockProfit::getStockCode, vo.getStockCode());
        wrapper.eq(vo.getFiscalYear() != null, StockProfit::getFiscalYear, vo.getFiscalYear());
        wrapper.orderByDesc(StockProfit::getCreateDate);
        IPage<StockProfit> page = super.page(new Page<>(vo.getCurrPage(), vo.getSize()), wrapper);

        // 提取股票代码
        List<String> stockCodes = page.getRecords().stream().map(StockProfit::getStockCode).distinct().collect(Collectors.toList());
        if (stockCodes.isEmpty()) {
            return new PageUtils<>(new ArrayList<>(), page.getTotal(), page.getSize(), page.getCurrent());
        }
        List<Stock> stocks = stockDAO.findCodeNameByCodes(stockCodes);
        Map<String, String> codeNameMap = stocks.stream().collect(Collectors.toMap(Stock::getCode, Stock::getName));

        List<StockProfitPageVO> targets = page.getRecords().stream().map(item -> {
            StockProfitPageVO pageVO = new StockProfitPageVO();
            pageVO.setId(item.getId());
            pageVO.setStockCode(item.getStockCode());
            pageVO.setStockName(codeNameMap.get(item.getStockCode()));
            pageVO.setFiscalYear(item.getFiscalYear());
            pageVO.setQ1NetProfit(item.getQ1NetProfit());
            pageVO.setQ1NoneNetProfit(item.getQ1NoneNetProfit());
            pageVO.setQ1Eps(item.getQ1Eps());
            pageVO.setQ2NetProfit(item.getQ2NetProfit());
            pageVO.setQ2NoneNetProfit(item.getQ2NoneNetProfit());
            pageVO.setQ2Eps(item.getQ2Eps());
            pageVO.setQ3NetProfit(item.getQ3NetProfit());
            pageVO.setQ3NoneNetProfit(item.getQ3NoneNetProfit());
            pageVO.setQ3Eps(item.getQ3Eps());
            pageVO.setQ4NetProfit(item.getQ4NetProfit());
            pageVO.setQ4NoneNetProfit(item.getQ4NoneNetProfit());
            pageVO.setQ4Eps(item.getQ4Eps());
            return pageVO;
        }).collect(Collectors.toList());

        return new PageUtils<>(targets, page.getTotal(), page.getSize(), page.getCurrent());
    }

    public List<StockProfit> findByStockCode(String stockCode) {
        LambdaQueryWrapper<StockProfit> wr = Wrappers.lambdaQuery();
        wr.eq(StockProfit::getStockCode, stockCode);
        wr.orderByAsc(StockProfit::getFiscalYear);
        return list(wr);
    }
}
